package ejb.manager;

import java.util.List;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.ejb.Stateless;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import ejb.entity.City;


@Stateless
public class DefaultMailManager implements MailManager {
	
	private ResourceBundle msg;
	private final String username;
	private final String password;

	public DefaultMailManager(){
		this.msg = ResourceBundle.getBundle("Bundle");		
		this.username = "twojemeteo@gmail.com";
		this.password = "twojemeteo";
	}
	
	public boolean send(String to, String content){
 
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
 
		try {
 
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(username));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
			message.setSubject(msg.getString("weather"));
			message.setText(content);
			Transport.send(message);
 
			System.out.println("Send");
 
		} catch (MessagingException e) {
			return false;
		}
		return true;
	}

	@Override
	public boolean send(String to, List<City> forecastCities){
 
		Properties props = new Properties();
		props.put("mail.smtp.auth", "true");
		props.put("mail.smtp.starttls.enable", "true");
		props.put("mail.smtp.host", "smtp.gmail.com");
		props.put("mail.smtp.port", "587");
 
		Session session = Session.getInstance(props,
		  new javax.mail.Authenticator() {
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(username, password);
			}
		  });
 
		try {
 
			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress(username));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
			message.setSubject(msg.getString("weather"));

			MimeMultipart forecast = new MimeMultipart();
			BodyPart title = new MimeBodyPart();
			String date = new DefaultDateConverter().numberToDate(1);
			title.setContent("<html>"+msg.getString("forecast") + " " + msg.getString("onDay") + date +"<br/>", "text/html; charset=UTF-8");
			String content = "<table><tr>";
			for(City c : forecastCities)
				content += "<td>" + c.getName() + "<br/>" + "<img src=\"http://localhost:8080/WeatherWEB/Image?name=" + c.getConditions() + "\" ><br/>" + msg.getString("temp") + (int)c.getTemperature() + msg.getString("celsius") + "</td>";

			content += "</tr></table>";
			BodyPart body = new MimeBodyPart();
			body.setContent(content, "text/html; charset=UTF-8");
			BodyPart end = new MimeBodyPart();
			end.setContent("</html>", "text/html; charset=UTF-8");

			forecast.addBodyPart(title);
			forecast.addBodyPart(body);
			forecast.addBodyPart(end);
			message.setContent(forecast);
			Transport.send(message);
 
			System.out.println("Send");
 
		} catch (MessagingException e) {
			return false;
		}
		return true;
	}
}
